package org.xjtu.weteam.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xjtu.weteam.common.JsonResponse;
import org.xjtu.weteam.common.utils.TokenUtils;
import org.xjtu.weteam.common.utils.imageUtil;
import org.xjtu.weteam.model.domain.Team;

import org.springframework.web.multipart.MultipartFile;
import org.xjtu.weteam.model.dto.TeamDTO;
import org.xjtu.weteam.service.TeamService;

import java.sql.Wrapper;
import java.util.List;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

/**
 * @author Kevin
 */
@RestController
@RequestMapping("/api/team")
public class TeamController {
    private final String path ="./file/image/";

    private final Logger logger = LoggerFactory.getLogger(TeamController.class);

    @Autowired
    private TeamService teamService;

    @PostMapping("/getJoinTeams")
    @ResponseBody
    public JsonResponse getJoinTeams(@RequestHeader("X-Access-Token") String token) throws IOException {
        List<Team> teams = teamService.getJoinTeams(token);
        return JsonResponse.success(teams);
    }

    @Autowired
    UserTeamController userTeamController;

    @PostMapping("/checkIfExists")
    @ResponseBody
    public JsonResponse checkIfExists(@RequestHeader("X-Access-Token") String token, Team team){
        Integer userId = TokenUtils.getIdFromToken(token);
        Team one = teamService.getById(team.getTeamId());
        if(one != null && Objects.equals(one.getTeamName(), team.getTeamName()) && userTeamController.checkIfIn(team.getTeamId(), userId) && one.getIsEnabled()){
            return JsonResponse.success(true);
        }
        return JsonResponse.success(false);
    }

    @PostMapping("/savePic")
    public JsonResponse savePic(@RequestParam("file") MultipartFile file){
        String avatarPath = null;
        try {
            avatarPath = imageUtil.saveAvatarFile(file);
        }catch (Exception e) {
            //e.printStackTrace();
            return JsonResponse.success("","文件保存失败");
        }
        return JsonResponse.success(avatarPath,"已完成上传");
    }
    @PostMapping("/create")
    @ResponseBody
    public JsonResponse create(TeamDTO teamDTO){
        if (TokenUtils.getIdFromToken(teamDTO.getToken())==null){
            return JsonResponse.success(false,"登录过期");
        }
        Team team=new Team();
        team.setTeamName(teamDTO.getTeamName());
        team.setDescription(teamDTO.getDescription());
        team.setImagePath(teamDTO.getImagePath());
        try {
            if (team.getImagePath()=="") {
                team.setImagePath("default.png");
            }
            teamService.createTeam(team,teamDTO.getToken());
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println(new File(path + teamDTO.getImagePath()).delete());
            return JsonResponse.success(false,"团队名称已存在");
        }
        return JsonResponse.success(true,"已完成创建");
    }
    @GetMapping("/getTeamById")
    @ResponseBody
    public JsonResponse getTeamById(Integer teamId) {
        Team team=teamService.getByTeamId(teamId);
        if (team!=null){
            return JsonResponse.success(team,"成功");
        }
        return JsonResponse.failure("找不到团队");
    }
    @PostMapping("/join")
    @ResponseBody
    public JsonResponse join(TeamDTO teamDTO){
//        System.out.println(teamDTO.getTeamId());
//        System.out.println(teamDTO.getToken());
        if (TokenUtils.getIdFromToken(teamDTO.getToken())==null){
            return JsonResponse.failure("登录失效");
        }
        boolean b=teamService.checkIfJoin(teamDTO);
        System.out.println(b);
        if(b){
            return JsonResponse.failure("已在团队中或团队已解散");
        }
        return JsonResponse.success(true,"成功加入");
    }
    @PostMapping("deleteTeam")
    @ResponseBody
    public JsonResponse delete(Integer teamId){
        teamService.deleteTeam(teamId);
        return JsonResponse.success(true);
    }
}
